package s;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Properties;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.mysql.jdbc.Statement;

import cbir.Thumb;


public class ShowAll extends HttpServlet {

	private static final long serialVersionUID = 1L;
	private Properties props;
	private String propsPath;

	@Override
	public void init(ServletConfig config) throws ServletException {
		super.init(config);
		
		this.propsPath = this.getServletContext().getRealPath("/");
		this.props = new Properties();
		try {
			props.loadFromXML(new FileInputStream(propsPath + "properties.properties"));
		} catch (FileNotFoundException e) {e.printStackTrace();} 
		catch (IOException e) {e.printStackTrace();}
}
	
	
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {

			int page = Integer.parseInt( req.getQueryString().substring(req.getQueryString().indexOf("=")+1, req.getQueryString().length()) );
	
			Connection connection = null;
			Statement statement;
			int numRighe = 0;
			
			try {
				Class.forName("com.mysql.jdbc.Driver");
				connection = DriverManager.getConnection(props.getProperty("DB_URL"), props.getProperty("DB_USER"), props.getProperty("DB_PASS"));
				statement = (Statement) connection.createStatement();
				ResultSet rs = statement.executeQuery("SELECT count(*) as c FROM img");
				 
				while(rs.next()){numRighe = rs.getInt("c");}
			} 
			catch (SQLException e1) {e1.printStackTrace();} catch (ClassNotFoundException e) {
				e.printStackTrace();
			}
			
			String response = initRss(numRighe, page, req);
			
			try {
				statement = (Statement) connection.createStatement();
				ResultSet rs = statement.executeQuery("SELECT Filename, Height, Width " +
																				"FROM img LIMIT " + page*100 + ", 100;");
			
				 while(rs.next()){
					response = response.concat( addItemRss( rs.getString("Filename"), rs.getInt("Height"), rs.getInt("Width"), req, props.getProperty("IMG_PATH")) );
				}
			} 
			catch (SQLException e2) {e2.printStackTrace();}
			finally {
				try {connection.close();} 
				catch (SQLException e) {e.printStackTrace();}
			}
	
			response = response.concat(endRss());
	
			try {connection.close();} catch (SQLException e) {e.printStackTrace();}
	
			PrintWriter pw =  res.getWriter();
			pw.print(response);
			System.out.println(response);
	}

	private String initRss(int n, int page, HttpServletRequest req){
		return "<?xml version='1.0' encoding='UTF-8' standalone='no'?>" +
							"<rss version='2.0' xmlns:cbir = 'http://sites.google.com/cbir-ppmm/' xmlns:atom='http://www.w3.org/2005/Atom'>" +
							"<channel>" +
							"<atom:link href='http://"+req.getServerName()+req.getContextPath()+"/showall' rel='self' type='application/rss+xml' />\n" +
							"<title>CBIR:ShowAll</title>" +
							"<link>http://sites.google.com/site/cbirppmm/</link>" +
							"<description>All the images in the database</description>" + 
							"<cbir:numResults>" + n + "</cbir:numResults>" + 
							"<cbir:currentPage>" + page + "</cbir:currentPage>";
	}
	
	private String endRss(){
		return "</channel>" +
							"</rss>";
	}
	
	private String addItemRss(String nomeFile, int altezza, int larghezza, HttpServletRequest req, String path) {
		return "<item>" +
							"<title>" + nomeFile + "</title>" +
							"<link>" + "http://" + req.getServerName() + req.getContextPath() + path + "thumb/" + nomeFile + "</link>" +
							"<cbir:thumbURL>" + "http://" + req.getServerName() + req.getContextPath() + path + "thumb/" + Thumb.getThumb(nomeFile) + "</cbir:thumbURL>" +
					"</item>";
	}
}